﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;

public class OrderBody{

    private byte[] bodyBuffer;  //消息体
    private byte[] headBuffer;  //消息头
    private byte[] userIDBuffer;//用户ID
    private int cmd;            //消息码
    private long userId;        //用户Id

    public byte[] BodyBuffer
    {
        set { bodyBuffer = value; }
        get { return bodyBuffer; }
    }

    public byte[] HeadBuffer
    {
        set { headBuffer = value; }
        get { return headBuffer; }
    }

    public int Cmd
    {
        set { cmd = value; }
        get { return cmd; }
    }

    public long UserId
    {
        set;
        get;
    }

    public byte[] GetNetBuffer()
    {
        byte[] totalBufer = BuidlBuffer();
        //List<byte> list = new List<byte>(totalBufer);
        //list.Reverse();
        //totalBufer = list.ToArray();
        return totalBufer;
    }

    /// <summary>
    /// 封包
    /// </summary>
    /// <returns></returns>
    private byte[] BuidlBuffer()
    {
        //消息码：4，用户ID:8，包体长度：4，包体： bodyBuffer.Length
        byte[] buffers = new byte[4 + 8 + 4 + bodyBuffer.Length];
        headBuffer = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(cmd));
        headBuffer.CopyTo(buffers, 0);      //消息码
        userIDBuffer = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Global.Instance.userId));
        userIDBuffer.CopyTo(buffers, 4);      //用户ID
        int bodyLength = bodyBuffer.Length;
        byte[] bodyLengthByte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(bodyLength));
        bodyLengthByte.CopyTo(buffers, 12);  //包体长度
        bodyBuffer.CopyTo(buffers, 16);      //包体
        return buffers;
    }

}
